package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursing_project")
@Api(tags = "护理项目管理")
public class NursingProjectController extends BaseController {
    @Autowired
    private NursingProjectService nursingProjectService;

    /**
     * 分页查询护理项目
     *
     * @param name         护理项目名称，用于模糊查询
     * @param pageNum      当前页码
     * @param pageSize     每页显示的记录数
     * @param status       护理项目状态（0：禁用，1：启用）
     * @return             分页查询结果
     */
    @ApiOperation("分页查询护理项目")
    @GetMapping
    public ResponseResult<PageResponse<NursingProjectVo>> getByPage(
            @ApiParam(value = "护理项目名称", required = false) @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示的记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @ApiParam(value = "护理项目状态（0：禁用，1：启用）", required = false) @RequestParam(value = "status", required = false) Integer status
    ) {
        PageResponse<NursingProjectVo> page = nursingProjectService.getPage(name, pageNum, pageSize, status);
        return success(page);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto 护理项目数据传输对象
     * @return                  操作结果
     */
    @ApiOperation("新增护理项目")
    @PostMapping
    public ResponseResult add(@RequestBody @ApiParam("护理项目信息") NursingProjectDto nursingProjectDto) {
        nursingProjectService.add(nursingProjectDto);
        return success();
    }

    /**
     * 根据ID获取护理项目详情
     *
     * @param id 护理项目ID
     * @return   护理项目详情
     */
    @ApiOperation("根据ID获取护理项目详情")
    @GetMapping("/{id}")
    public ResponseResult<NursingProjectVo> getById(@PathVariable("id") @ApiParam("护理项目ID") Long id) {
        NursingProjectVo nursingProjectVo = nursingProjectService.getById(id);
        return success(nursingProjectVo);
    }

    /**
     * 根据ID修改护理项目
     *
     * @param nursingProjectDto 护理项目数据传输对象
     * @return                  操作结果
     */
    @ApiOperation("根据ID修改护理项目")
    @PutMapping
    public ResponseResult update(@RequestBody @ApiParam("护理项目信息") NursingProjectDto nursingProjectDto) {
        nursingProjectService.update(nursingProjectDto);
        return success();
    }

    /**
     * 修改状态（启用、禁用）
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    public ResponseResult enableOrDisable(
            @PathVariable("id") Long id,
            @PathVariable("status") Integer status){
        nursingProjectService.enableOrDisable(id, status);
        return success();
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @ApiOperation("查询所有护理项目")
    @GetMapping("/all")
    public ResponseResult<List<NursingProjectVo>> getAllNursingProject(){
        return success(nursingProjectService.listAll());
    }

    /**
     * 根据id删除护理项目
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable Long id){
        NursingProjectVo nursingProjectVo = nursingProjectService.getById(id);
        if(nursingProjectVo == null){
            return error();
        }
        nursingProjectService.deleteById(id);
        return success();
    }
}
